package com.yjf.ljr.util;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.yjf.ljr.R;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.connection.IjConnect;
import com.yjf.ljr.dataclass.SendSmsCodeClass;
import com.yjf.ljr.parser.RequestBuilder;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;

public class ViewCodeDialog extends Dialog {

	public ViewCodeDialog(Context context) {
		super(context);
	}

	public ViewCodeDialog(Context context, int theme) {
		super(context, theme);
	}

	public static class Builder {
		private Context context;
		private String title;
		private String data;
		private String positiveButtonText;
		private String negativeButtonText;
		private String webviewButtonText;
		private View contentView;
		private String ViewCode;// 验证码
		private EditText mEtViewCode;// 验证码
		private TextView mTvPhonebind;//
		// private WebView mWebView;// 验证码
		private Button mBtGetview;
		private Context mCtx;

		private DialogInterface.OnClickListener positiveButtonClickListener;
		private DialogInterface.OnClickListener negativeButtonClickListener;
		private DialogInterface.OnClickListener webviewButtonClickListener;

		public Builder(Context context) {
			this.context = context;
		}

		public Builder setViewcode(String data) {
			this.data = data;
			return this;
		}

		/**
		 * Set the Dialog message from resource
		 * 
		 * @param title
		 * @return
		 */
		public Builder setViewcode(int data) {
			this.data = (String) context.getText(data);
			return this;
		}

		/**
		 * Set the Dialog title from resource
		 * 
		 * @param title
		 * @return
		 */
		public Builder setTitle(int title) {
			this.title = (String) context.getText(title);
			return this;
		}

		/**
		 * Set the Dialog title from String
		 * 
		 * @param title
		 * @return
		 */

		public Builder setTitle(String title) {
			this.title = title;
			return this;
		}

		/**
		 * Set the Dialog title from String
		 * 
		 * @param title
		 * @return
		 */

		public String getViewCode() {
			if (TextUtils.isEmpty(mEtViewCode.getText().toString())) {
				return "";
			} else {
				return mEtViewCode.getText().toString();
			}
		}

		public Builder setContentView(View v) {
			this.contentView = v;
			return this;
		}

		/**
		 * Set the positive button resource and it's listener
		 * 
		 * @param positiveButtonText
		 * @return
		 */
		public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) {
			this.positiveButtonText = (String) context.getText(positiveButtonText);
			this.positiveButtonClickListener = listener;
			return this;
		}

		public Builder setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) {
			this.positiveButtonText = positiveButtonText;
			this.positiveButtonClickListener = listener;
			return this;
		}

		public Builder reFresh(String webButtonText, Context mCtx, DialogInterface.OnClickListener listener) {
			this.webviewButtonText = webButtonText;
			this.webviewButtonClickListener = listener;
			this.mCtx = mCtx;
			return this;
		}

		public Builder setNegativeButton(int negativeButtonText, DialogInterface.OnClickListener listener) {
			this.negativeButtonText = (String) context.getText(negativeButtonText);
			this.negativeButtonClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(String negativeButtonText, DialogInterface.OnClickListener listener) {
			this.negativeButtonText = negativeButtonText;
			this.negativeButtonClickListener = listener;
			return this;
		}

		class TimeCount extends CountDownTimer {
			public TimeCount(long millisInFuture, long countDownInterval) {
				super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
			}

			@Override
			public void onFinish() {// 计时完毕时触发
				mBtGetview.setText("重新发送");
				mBtGetview.setClickable(true);
			}

			@Override
			public void onTick(long millisUntilFinished) {// 计时过程显示
				mBtGetview.setClickable(false);
				mBtGetview.setText(millisUntilFinished / 1000 + "秒后可重新发送");
			}
		}

		public ViewCodeDialog create() {
			final TimeCount time;// 计时
			time = new TimeCount(60000, 1000);// 构造CountDownTimer对象
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			// instantiate the dialog with the custom Theme
			final ViewCodeDialog dialog = new ViewCodeDialog(context, R.style.Dialog);
			View layout = inflater.inflate(R.layout.dialog_code_layout, null);
			dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			// set the dialog title
			((TextView) layout.findViewById(R.id.title)).setText(title);
			// mWebView = ((WebView) layout.findViewById(R.id.wbwebview));
			// mWebView.loadData(data, "text/html", "UTF-8");
			mEtViewCode = (EditText) layout.findViewById(R.id.etnewviewcode);
			mBtGetview = (Button) layout.findViewById(R.id.btgetview);
			mTvPhonebind = (TextView) layout.findViewById(R.id.tvphone_bind);
			// set the confirm button
			if (webviewButtonText == null) {
				if (webviewButtonClickListener != null) {
					(((Button) layout.findViewById(R.id.btgetview))).setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							webviewButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
							// mWebView.loadData(data, "text/html", "UTF-8");
							mTvPhonebind.setText("绑定手机：" + SPreferences.getData(mCtx, "", "investphone").toString());
							new SendSmsCodeTask(SPreferences.getData(mCtx, "", "investphone").toString()).execute();
							time.start();

						}
					});
				}
			} else {
				// if no confirm button just set the visibility to GONE
				// layout.findViewById(R.id.wbwebview).setVisibility(View.GONE);
			}
			// set the confirm button
			if (positiveButtonText != null) {
				((Button) layout.findViewById(R.id.positiveButton)).setText(positiveButtonText);
				if (positiveButtonClickListener != null) {
					((Button) layout.findViewById(R.id.positiveButton)).setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
						}
					});
				}
			} else {
				// if no confirm button just set the visibility to GONE
				layout.findViewById(R.id.positiveButton).setVisibility(View.GONE);
			}
			// set the cancel button
			if (negativeButtonText != null) {
				((Button) layout.findViewById(R.id.negativeButton)).setText(negativeButtonText);
				if (negativeButtonClickListener != null) {
					((Button) layout.findViewById(R.id.negativeButton)).setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							// mWebView.loadData(data, "text/html", "UTF-8");
							negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
						}
					});
				}
			} else {
				// if no confirm button just set the visibility to GONE
				layout.findViewById(R.id.negativeButton).setVisibility(View.GONE);
			}
			// set the content message
			/*
			 * if (message != null) { ((TextView)
			 * layout.findViewById(R.id.message)).setText(message); } else if
			 * (contentView != null) { // if no message set // add the
			 * contentView to the dialog body ((LinearLayout)
			 * layout.findViewById(R.id.message)).removeAllViews();
			 * ((LinearLayout)
			 * layout.findViewById(R.id.message)).addView(contentView, new
			 * LayoutParams( LayoutParams.WRAP_CONTENT,
			 * LayoutParams.WRAP_CONTENT)); }
			 */
			dialog.setContentView(layout);
			return dialog;
		}

		class SendSmsCodeTask extends AsyncTask<Void, Void, Void> {
			private String mobile;// 电话号码
			private String mFailLog;
			private SendSmsCodeClass dateSendSms = new SendSmsCodeClass();

			public SendSmsCodeTask(String mobile) {
				this.mobile = mobile;
			}

			@Override
			protected Void doInBackground(Void... arg0) {
				// TODO Auto-generated method stub
				RequestObject requestObjectSms = new RequestObject();
				requestObjectSms.method = "sendSmsCode";
				requestObjectSms.map.put("mobile", mobile);
				requestObjectSms.map.put("business", "cellphone");

				String requestSmsUrl = RequestBuilder.build(requestObjectSms);
				try {
					IjConnect.doRequest(requestSmsUrl, dateSendSms);
					if (dateSendSms.code == null) {
						mFailLog = "获取数据异常，请稍后重试";
					} else if (!dateSendSms.code.equals(CommonData.RESULT_SUCCESS)
							&& !("1".equals(dateSendSms.isSuccess))) {
						mFailLog = dateSendSms.msg + "dateSendSms.msg";
					}
				} catch (Exception e) {
					// TODO: handle exception
					mFailLog = "获取数据异常，请稍后重试";
					e.printStackTrace();
				}
				return null;
			}

			@Override
			protected void onPostExecute(Void result) {
				// TODO Auto-generated method stub
				if ("-1".equals(dateSendSms.code)) {

					return;
				}
				if (!TextUtils.isEmpty(mFailLog)) {
					Toast.makeText(mCtx, mFailLog, Toast.LENGTH_LONG).show();
				} else {
					if (dateSendSms.code != null && dateSendSms.code.equals(CommonData.RESULT_SUCCESS)) {
						// TODO GET Verification code
						Toast.makeText(mCtx, dateSendSms.msg, Toast.LENGTH_LONG).show();
					}
				}
			}
		}
	}

}
